﻿using UnityEngine;
using System.Collections.Generic;
using System;
using Dict.DataModel;
using ProtoBuf.Message;
using Dict.Blo;

namespace Room
{
    public class RCAITower : RCAIUnionFightBase
    {
        #region 数据
        public TableUnionFightTower dictData;
        public InstUFTower instData;
        #endregion


        bool isAttacking;
        GameObject shell;
        Vector3 from;
        RoomCharactor target;
        float delay = 0f;
        float timer = 0f;

        public Vector3 firePoint;

        public RCAITower(RoomCharactor owner)
            : base(owner)
        {
            this.type = AIType.UnionTower;
        }

        public void Init(InstUFTower inst)
        {
            UpdateData(inst);

            InitFSM();
        }


        public void UpdateData(InstUFTower inst)
        {
            this.instData = inst;
            this.dictData = DictUnionFightTowerBlo.GetTowerByID(inst.id + "");
            this.camp = dictData.Camp;
            this.firePoint = DictUnionFightTowerBlo.GetFirePoint(inst.id + "");
        }

        public void Attack(RoomCharactor target,float delay=1f)
        {
            if (shell==null)
            {
                if (camp == 0)
                {
                    shell = Spawn(RoomUnionFight.SAO_PROJECTILE);
                }
                else
                {
                    shell = Spawn(RoomUnionFight.ALO_PROJECTILE);
                }
            }
            else
            {
                shell.SetActive(true);
            }

            shell.transform.position = firePoint;
            if (camp == 0)
            {
                var obj = Spawn(RoomUnionFight.SAO_TOWER_FIRE, delay);
                if(obj!=null)
                {
                    obj.transform.position = firePoint;
                }
            }

            isAttacking = true;
            this.target = target;
            this.delay = delay;
            this.timer = 0f;
            this.from = shell.transform.position;
        }
        
        void AttackAni()
        {
            if (isAttacking)
            {
                this.timer += Time.deltaTime;
                var to = target.transform.position+new Vector3(0,1f,0);
                var cur = Vector3.Lerp(from, to, timer/delay );

                shell.transform.position = cur;

                if (timer >= delay)
                {
                    isAttacking = false;
                    Unspawn(shell);

                    shell = null;
                }
            }
        }

        public void SetEnableEffect(bool actived)
        {
            if (sceneObj != null)
            {
                var particles = sceneObj.GetComponentsInChildren<ParticleSystem>();
                if (particles != null)
                {
                    foreach (var parti in particles)
                    {
                        parti.gameObject.SetActive(actived);
                    }
                }

                var tweenRotations = sceneObj.GetComponentsInChildren<TweenRotation>();
                if (tweenRotations != null)
                {
                    foreach (var rot in tweenRotations)
                    {
                        rot.enabled = actived;
                    }
                }
            }
        }
        
        #region FSM
        private UnionTowerState currState = UnionTowerState.None;
        public UnionTowerState CurrState
        {
            get
            {
                return currState;
            }
        }

        private Dictionary<UnionTowerState, RCAITowerBase> allState = new Dictionary<UnionTowerState, RCAITowerBase>();

        public RCAITowerBase currentState;

        public override void Tick()
        {
            base.Tick();
            if (currentState != null)
                currentState.Tick();

            AttackAni();
        }

        private void InitFSM()
        {
            allState[UnionTowerState.None] = new RCAITowerBase();
            allState[UnionTowerState.Actived] = new RCAITowerActived();
            allState[UnionTowerState.Unactived] = new RCAITowerUnactived();

            foreach (var state in allState)
            {
                state.Value.controller = this;
            }
            SwitchState((int)UnionTowerState.Unactived);
        }

        public override void SwitchState(int newState)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionTowerState)newState;
                allState[currState].Enter();
                currentState = allState[currState];
            }
        }


        #endregion

        public override void DoStop()
        {
            base.DoStop();
            if(shell!=null)
            {
                Unspawn(shell);
            }
        }
    }
}